using System;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for MyStream.
	/// </summary>
	public class MyStream 
		: System.IO.Stream
	{
		public string output;
		private System.IO.Stream baseStream;
		private bool logMessages = false;
		private SoapRecord soapRecord;

		public SoapRecord SoapLog 
		{
			get { return soapRecord; }
			set { soapRecord = value; }
		}
		public MyStream(System.IO.Stream stream, bool LogMessages)
		{
			//
			// TODO: Add constructor logic here
			//
			baseStream = stream;
			logMessages = LogMessages;
			output = "";
		}

		~MyStream() 
		{
			//System.Diagnostics.Trace.WriteLine("********  NEW RESPONSE  ********" + System.Environment.NewLine + output + System.Environment.NewLine + System.Environment.NewLine );
		}
		public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			return baseStream.BeginRead (buffer, offset, count, callback, state);
		}

		public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			return baseStream.BeginWrite (buffer, offset, count, callback, state);
		}
		public override bool CanRead
		{
			get
			{
				return baseStream.CanRead;
			}
		}

		public override bool CanSeek
		{
			get
			{
				return baseStream.CanSeek;
			}
		}

		public override bool CanWrite
		{
			get
			{
				return baseStream.CanWrite;
			}
		}

		public override void Close()
		{
			baseStream.Close ();
		}

		public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
		{
			return baseStream.CreateObjRef (requestedType);
		}
		protected override System.Threading.WaitHandle CreateWaitHandle()
		{
			return base.CreateWaitHandle ();
		}
		public override int EndRead(IAsyncResult asyncResult)
		{
			return baseStream.EndRead (asyncResult);
		}
		public override void EndWrite(IAsyncResult asyncResult)
		{
			baseStream.EndWrite (asyncResult);
		}
		public override bool Equals(object obj)
		{
			return baseStream.Equals (obj);
		}
		public override void Flush()
		{
			baseStream.Flush();
		}

		public override int GetHashCode()
		{
			return baseStream.GetHashCode ();
		}

		public override object InitializeLifetimeService()
		{
			return baseStream.InitializeLifetimeService ();
		}

		public override long Length
		{
			get
			{
				return baseStream.Length;
			}
		}
		public override long Position
		{
			get
			{
				return baseStream.Position;
			}
			set
			{
				baseStream.Position = value;
			}
		}
		public override int Read(byte[] buffer, int offset, int count)
		{
			int ret = baseStream.Read(buffer, offset, count);
			if (logMessages) 
			{
				if (soapRecord == null) soapRecord = new SoapRecord();
				output += System.Text.Encoding.ASCII.GetString(buffer, offset, ret);
				output = output.Replace("\0", "");
				soapRecord.Response = output;
				if (ret ==0) 
				{
					System.Diagnostics.Trace.WriteLine("+++++++++++++++++ REQUEST START +++++++++++++++++++++++");
					System.Diagnostics.Trace.WriteLine(System.Environment.NewLine + System.DateTime.Now.ToString());
					System.Diagnostics.Trace.WriteLine("BEGINSOAPRESPONSE" + output);
					System.Diagnostics.Trace.WriteLine("+++++++++++++++++  REQUEST END  +++++++++++++++++++++++" + System.Environment.NewLine);
				}
			}
			return ret;
		}
		public override int ReadByte()
		{
			return baseStream.ReadByte ();
		}
		public override long Seek(long offset, System.IO.SeekOrigin origin)
		{
			return baseStream.Seek(offset, origin);
		}
		public override void SetLength(long value)
		{
			baseStream.SetLength(value);
		}
		public override string ToString()
		{
			return baseStream.ToString ();
		}
		public override void WriteByte(byte value)
		{
			baseStream.WriteByte (value);
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			if (logMessages) 
			{
				output += System.Text.Encoding.ASCII.GetString(buffer, offset, count);
//				if (soapRecord == null) soapRecord = new SoapRecord();
//				soapRecord.Request = output;
			}
			baseStream.Write(buffer, offset, count);
			
		}



	}
}
